package com.passive.lock.service.auth.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.passive.lock.mapper.TAuthMapper;
import com.passive.lock.mapper.TAuthMapperCustom;
import com.passive.lock.mapper.TKeytolockMapper;
import com.passive.lock.model.po.TAuth;
import com.passive.lock.model.po.TKeytolock;
import com.passive.lock.model.vo.TAuthVo;
import com.passive.lock.service.auth.AuthService;
import com.passive.lock.utils.DateUtils;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.JsonUtils;
import com.passive.lock.utils.Result;

@Service("authService")
public class AuthServiceImpl implements AuthService {
	@Resource(name = "TAuthMapper")
	private TAuthMapper authMapper;
	@Resource(name = "TAuthMapperCustom")
	private TAuthMapperCustom authMapperCustom;
	
	@Resource(name = "TKeytolockMapper")
	private TKeytolockMapper keytolockMapper;

	@Override
	public Result deleteAuth(TAuthVo tAuthVo) {
		int count = authMapper.deleteByPrimaryKey(tAuthVo.getAuthid());
		return Result.ok(count);
	}

	@Override
	public Result deleteAuthList(List<Integer> tAuthids) {
		int counts = 0;
		for (Integer tAuthid : tAuthids) {
			int count = authMapper.deleteByPrimaryKey(tAuthid);
			counts = counts + count;
		}
		return Result.ok(counts);
	}

	@Override
	public Result updateAuth(TAuthVo tAuthVo) {
		tAuthVo.setAuthtime(System.currentTimeMillis());
		int count = authMapper.updateByPrimaryKey(tAuthVo);
		return Result.ok(count);
	}

	@Override
	public Result addAuth(TAuthVo tAuthVo) {
		tAuthVo.setAuthtime(System.currentTimeMillis());
		TAuth auth = new TAuth();
		DozerMapperUtils.map(tAuthVo, auth);
		int count = authMapper.insert(auth);
		String string = tAuthVo.getLockids();
		String [] stringArr= string.split(",");
		List<String> list = java.util.Arrays.asList(stringArr);
		for (String lockid : list) {
			TKeytolock keytolock = new TKeytolock();
			keytolock.setKeyid(tAuthVo.getKeyid());
			keytolock.setLockid(Integer.parseInt(lockid));
			keytolock.setAuthid(auth.getAuthid());
			keytolockMapper.insert(keytolock);
		}
		return Result.ok(count);
	}

	@Override
	public List<TAuthVo> authList(String groupid, long beginTime, long endTime, String authname, String username,
			String employeename, String authtype, int offset, Integer limit) {
		List<TAuthVo> list = authMapperCustom.authList(beginTime, endTime, groupid, authname, username, employeename,
				authtype, offset, limit);
		for (TAuthVo tAuthVo : list) {
			if(tAuthVo.getLockids()==null||"".equals(tAuthVo.getLockids())){
				continue;
			}
			String[] stringArr = tAuthVo.getLockids().split(",");
			
			List<Integer> lockids = new ArrayList<>();
			for (String string : stringArr) {
				lockids.add(Integer.parseInt(string));
			}
			tAuthVo.setLockids(JsonUtils.objectToJson(lockids));
			tAuthVo.setAuthstarttimevo(DateUtils.unixTimestampToDate(tAuthVo.getAuthstarttime()));
			tAuthVo.setAuthstoptimevo(DateUtils.unixTimestampToDate(tAuthVo.getAuthstoptime()));
			tAuthVo.setAuthtimevo(DateUtils.unixTimestampToDate(tAuthVo.getAuthtime()));
		}
		return list;
	}

	@Override
	public int authListCount(String groupid, long beginTime, long endTime, String authname, String username,
			String employeename, String authtype) {
		int count = authMapperCustom.authListCount(beginTime, endTime, groupid, authname, username, employeename,
				authtype);
		return count;
	}

}
